﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IPC.Communication.Framework.Core.IMessage
{
    public class MelsecA1EAsciiMessage : INetMessage
    {
        public int ProtocolHeadBytesLength
        {
            get
            {
                return 4;
            }
        }

        public byte[] HeadBytes
        {
            get;
            set;
        }

        public byte[] ContentBytes
        {
            get;
            set;
        }

        public byte[] SendBytes
        {
            get;
            set;
        }

        public int GetContentLengthByHeadBytes()
        {
            bool flag = this.HeadBytes[2] == 53 && this.HeadBytes[3] == 66;
            int result;
            if (flag)
            {
                result = 4;
            }
            else
            {
                bool flag2 = this.HeadBytes[2] == 48 && this.HeadBytes[3] == 48;
                if (flag2)
                {
                    int num = Convert.ToInt32(Encoding.ASCII.GetString(this.SendBytes, 20, 2), 16);
                    bool flag3 = num == 0;
                    if (flag3)
                    {
                        num = 256;
                    }
                    switch (this.HeadBytes[1])
                    {
                        case 48:
                            result = ((num % 2 == 1) ? (num + 1) : num);
                            break;
                        case 49:
                            result = num * 4;
                            break;
                        case 50:
                        case 51:
                            result = 0;
                            break;
                        default:
                            result = 0;
                            break;
                    }
                }
                else
                {
                    result = 0;
                }
            }
            return result;
        }

        public bool CheckHeadBytesLegal(byte[] token)
        {
            bool flag = this.HeadBytes != null;
            return flag && this.HeadBytes[0] - this.SendBytes[0] == 8;
        }

        public int GetHeadBytesIdentity()
        {
            return 0;
        }
    }
}
